/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Sessions;

import entityBeans.*;
import java.rmi.RemoteException;
import java.util.List;
import javax.ejb.SessionContext;
import javax.ejb.Stateful;
import javax.persistence.*;

/**
 *
 * @author BELALOUACHE
 */
@Stateful
public class EcomAdmin implements EcomAdminRemote {

    private static final String PERSISTENCE_UNIT_NAME = "StoreEcom-ejbPU";
    private EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
    private SessionContext context;
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    @Override
    public void createCust(String name, String fistName, String mail, String pwd, String num_cmp, Double solde, String adresse) throws RemoteException {
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        // Create a new Account
        Accounts cmpClient = new Accounts();
        cmpClient.setIdAccount(num_cmp);
        cmpClient.setSolde(solde);
        // Create a new customers
        Customers client = new Customers();
        client.setFistName(fistName);
        client.setName(name);
        client.setMail(mail);
        client.setPwd(pwd);
        client.setAdress(adresse);
        client.setIdAccountFk(cmpClient);
        em.persist(cmpClient);
        em.persist(client);
        // Commit the transaction, which will cause the entity to
        // be stored in the database
        System.out.println("Your customer was successfully created");

        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        em.close();
    }

    @Override
    public void removeCust(Integer idCust) throws RemoteException {
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        Customers client = em.find(Customers.class, idCust);
        Accounts cmp = em.find(Accounts.class, client.getIdAccountFk().getIdAccount());
        em.remove(client);
        em.remove(cmp);
        // Commit the transaction, which will cause the entity to
        // be stored in the database
        System.out.println("Your customer was successfully canceled");

        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        em.close();
    }

    @Override
    public List<Customers> getAllClient() throws RemoteException {
        List<Customers> lesClients;
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        Query q = em.createNamedQuery("Customers.findAll");
        lesClients = q.getResultList();
        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        em.close();
        return lesClients;
    }

    @Override
    public List<Customers> getClientByname(String name) throws RemoteException {
        List<Customers> lesClients = null;
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        Query q = em.createQuery("SELECT c FROM Customers c WHERE c.name = :name");
        q.setParameter("name", name);
        lesClients = q.getResultList();
        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        em.close();
        return lesClients;
    }

    @Override
    public Customers getClientById(Integer idClient) throws RemoteException {
        EntityManager em = factory.createEntityManager();
        Customers client = null;
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        client = em.find(Customers.class, idClient);
        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        em.close();
        return client;
    }

    @Override
    public void createProd(String name, double price, Category cat, Productsstore store) throws RemoteException {
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        Products prod = new Products();
        prod.setNameProd(name);
        prod.setPriceProd(price);
        prod.setIdProductsstoreFk(store);
        prod.setIdCategoryFk(cat);
        em.persist(prod);
        // Commit the transaction, which will cause the entity to
        // be stored in the database
        System.out.println("Your product was successfully created");

        // It is always good practice to close the EntityManager so that
        // resources are conserved.

        em.close();
    }

    @Override
    public void removeProd(Integer Pkprod) throws RemoteException {
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        Products prod = em.find(Products.class, Pkprod);
        em.remove(prod);
        // Commit the transaction, which will cause the entity to
        // be stored in the database
        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        System.out.println("Your product was successfully canceled");

        em.close();

    }

    @Override
    public void createStore(String name, String num_comp, double solde, String adress) throws RemoteException {
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        // Create a new Account
        Accounts cmpStore = new Accounts();
        cmpStore.setIdAccount(num_comp);
        cmpStore.setSolde(solde);
        // Create a new store
        Productsstore store = new Productsstore();
        store.setName(name);
        store.setAdress(adress);
        store.setIdAccountFk(cmpStore);
        em.persist(cmpStore);
        em.persist(store);
        // Commit the transaction, which will cause the entity to
        // be stored in the database

        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        System.out.println("Your store was successfully created");

        em.close();
    }

    @Override
    public void removeStore(Integer idStore) throws RemoteException {
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        Productsstore store = em.find(Productsstore.class, idStore);
        // Read the existing entries and write to console
        Query q = em.createQuery("select referenceProd from products where=:id_productsstore_fk");
        q.setParameter("id_productsstore_fk", idStore);
        List<Products> lesProd = q.getResultList();
        for (Products produit : lesProd) {
            em.remove(produit);
        }

        Accounts cmp = em.find(Accounts.class, store.getIdAccountFk().getIdAccount());
        em.remove(cmp);
        em.remove(store);
        // Commit the transaction, which will cause the entity to
        // be stored in the database
        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        System.out.println("Your category was successfully canceled");

        em.close();
    }

    @Override
    public void createCat(String name) throws RemoteException {
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        // Create a new Category of product
        Category cat = new Category();
        cat.setNameCategory(name);
        em.persist(cat);
        // Commit the transaction, which will cause the entity to
        // be stored in the database
        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        System.out.println("Your category was successfully created");

        em.close();
    }

    @Override
    public List<Accounts> getAccounts() throws RemoteException {
        List<Accounts> lesComptes;
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        Query q = em.createQuery("SELECT a FROM Accounts a");
        lesComptes = q.getResultList();
        return lesComptes;
    }

    @Override
    public Category getCat(int idCat) throws RemoteException {
        Category cat = null;
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        cat = em.find(Category.class, idCat);
        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        em.close();
        return cat;
    }

    @Override
    public Productsstore getStore(int idStore) throws RemoteException {
        Productsstore store = null;
        EntityManager em = factory.createEntityManager();
        // Begin a new local transaction so that we can persist a new entity
        em.getTransaction().begin();
        store = em.find(Productsstore.class, idStore);
        // It is always good practice to close the EntityManager so that
        // resources are conserved.
        em.close();
        return store;
    }
}
